package bean;

import java.io.File;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.PriorityOrdered;

public class DirectoryBeanPostProcessor implements BeanPostProcessor,PriorityOrdered {
	private int order;
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		//过滤一下：只对需要创建目录的bean处理
		if (bean instanceof IDirectoryInfo){
			String path = ((IDirectoryInfo)bean).getPath();
			File file = new File(path);
			if ( !file.exists() ){
				file.mkdirs();
			}
		}
		
		//必须返回Bean的实例！
		return bean;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		return bean;
	}

	@Override
	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

}
